<?php

declare (strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Db;
use Hyperf\Utils\Arr;
use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property int $channel 
 * @property int $type 
 * @property string $link_url 
 * @property int $flag 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 */
class AppStandard extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'app_standard';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'channel' => 'integer', 'type' => 'integer', 'flag' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];
    const TYPE = [
        '0' => '用户协议',
        '1' => '隐私政策',
        '2'=>'平台公约'
    ];
    /**
     * 获取隐私政策和用户协议，根据渠道
     * @param int $channel
     */
    public static function getListByChannel($channel=0,$checkchannel=0)
    {
        $channel = $channel<= 0 ? 100: $channel;
        $checkchannel = $checkchannel<=0 ?  0 : $checkchannel;
        $ret = [
            'agreement_url' => [
                'name' => '《用户协议》',
                'link_url'=> self::getUrl(0,$channel,$checkchannel),
            ],
            'policy_url' => [
                'name' => '《隐私政策》',
                'link_url'=>self::getUrl(1,$channel,$checkchannel),
            ],
            'sys_rule_url' => [
                'name' => '平台公约',
                'link_url'=> self::getUrl(2,$channel,$checkchannel),
            ]
        ];
        return $ret;
    }

    /**
     * 获取协议
     * @param int $type
     * @param int $channel
     * @param int $checkchannel
     * @return \Hyperf\Utils\HigherOrderTapProxy|mixed|string|void
     */
    public static function getUrl($type=0,$channel=0,$checkchannel=0)
    {
        $info = self::where('channel',$channel)
            ->whereRaw("FIND_IN_SET({$checkchannel},check_channel)")
            ->where('flag',1)
            ->where('type',$type)
            ->orderBy('id','desc')
            ->first();
        if(empty($info)){
            $info = self::where('channel',$channel)
                ->where('flag',1)
                ->where('type',$type)
                ->where('check_channel',0)
                ->orderBy('id','desc')
                ->first();
        }
        $url = !empty($info) ? $info['link_url'] : '';
        return $url ;
    }

}